﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.BasicData.Clients;
using WMS.BasicData.Vendors;
using WMS.Common;
using WMS.StorageManagement.BinLocationDefinitions.DTO;
using WMS.StorageManagement.DTO;

namespace WMS.StorageManagement.BinLocationDefinitions
{
    public class BinLocationDefinitionAppService : IApplicationService, IBinLocationDefinitionAppService
    {
        /// <summary>
        /// 供应商仓储
        /// </summary>
        private readonly IRepository<BinLocationDefinition, int> _binLocationDefinitionRepository;


        /// <summary>
        /// 客户仓储接口
        /// </summary>
        private readonly IRepository<Client,int> _clientRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        public BinLocationDefinitionAppService(IRepository<BinLocationDefinition, int> binLocationDefinitionRepository, IRepository<Client, int> clientRepository)
        {
            _binLocationDefinitionRepository = binLocationDefinitionRepository;
            _clientRepository = clientRepository;
        }

        /// <summary>
        /// 可用储位
        /// </summary>
        /// <returns></returns>
        public async Task<PagedResult> GetEmptySpace(int page, int limit, string? customer)
        {
            // 获取并等待仓库储位定义列表
            var binLocationDefinitionList = await _binLocationDefinitionRepository.GetListAsync();
            // 将列表转换为可查询对象
            var binLocationDefinitionQueryable = binLocationDefinitionList.AsQueryable();

            // 获取并等待客户列表
            var clientList = await _clientRepository.GetListAsync();
            // 将列表转换为可查询对象
            var clientQueryable = clientList.AsQueryable();


            var binLocationDefinition = from a in binLocationDefinitionQueryable
                                        join b in clientQueryable
                                        on a.Customer equals b.Id
                                        select new EmptySpaceDTO
                                        {
                                            Id = a.Id,
                                            RepositoryId = a.RepositoryId,
                                            TheLocationType = a.TheLocationType,
                                            TheLocationCode = a.TheLocationCode,
                                            Customer = b.ChineseName,
                                            MaxVolume = a.MaxVolume,
                                            ListingOrder = a.ListingOrder,
                                            Wide = a.Wide,
                                            Long = a.Long,
                                            High = a.High,
                                            LargestPallets = a.LargestPallets,
                                            MaxWeight = a.MaxWeight,
                                            Xcoordinates = a.Xcoordinates,
                                            Ycoordinates = a.Ycoordinates,
                                            Zcoordinates = a.Zcoordinates,
                                            BinLocationAttributes = a.BinLocationAttributes,
                                        };

            binLocationDefinition = binLocationDefinition.WhereIf(!string.IsNullOrEmpty(customer), m => m.Customer == customer);
           

            var pagedData = binLocationDefinition.Skip((page - 1) * limit).Take(limit);

            // 获取总数
            int totalCount = binLocationDefinition.Count();

            PagedResult pageResult = new PagedResult
            {
                CurrentPage = page,
                PageCount = (int)Math.Ceiling((double)totalCount / limit),
                RowCount = totalCount,
                Queryable = pagedData // 若PagedResult需要IQueryable类型，可以先调用AsQueryable()
            };

            return await Task.FromResult(pageResult);
        }



        /// <summary>
        /// 可用储位
        /// </summary>
        /// <returns></returns>
        public async Task<List<Client>> GetClient()
        {
            var list = System.Linq.PredicateBuilder.New<Client>(true);
            var client =await  _clientRepository.GetListAsync(list);
            return client;
        }

        /// <summary>
        /// 创建储位定义
        /// </summary>
        /// <param name="CreateBinDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> CreateBinLocationDefi(BinLocationDefiDTO createBinDTO)
        {
            var binLocationDefilist = await _binLocationDefinitionRepository.GetListAsync(m => m.TheLocationCode == createBinDTO.TheLocationCode);
            if (binLocationDefilist.Count > 0) return -1;

            var binlocation= createBinDTO.MapTo<BinLocationDefinition>();
            await _binLocationDefinitionRepository.InsertAsync(binlocation);
            return 1;
        }

        /// <summary>
        /// 删除储位定义
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> DeleteBinLocationDefi(int id)
        {
            await _binLocationDefinitionRepository.DeleteAsync(m => m.Id == id);
            return 1;
        }

        /// <summary>
        /// 获取储位定义
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<BinLocationDefiDTO> GetBinLocationDefiById(int id)
        {
           var binlocationlist =await _binLocationDefinitionRepository.FirstOrDefaultAsync(m => m.Id == id);
            var binlocationDTO= binlocationlist.MapTo<BinLocationDefiDTO>();
            return binlocationDTO;
        }

        /// <summary>
        /// 修改储位定义列表
        /// </summary>
        /// <param name="updateBinDTO"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> UpdateBinLocationDefi(BinLocationDefiDTO updateBinDTO)
        {
            var binlocationlist = _binLocationDefinitionRepository.GetListAsync(m => m.Id == updateBinDTO.Id&&m.TheLocationCode==updateBinDTO.TheLocationCode).Result;
            if (binlocationlist.Count() > 0) return -1;

            var binlocationlists= updateBinDTO.MapTo<BinLocationDefinition>();
            await _binLocationDefinitionRepository.UpdateAsync(binlocationlists);
            return 1;

        }
    }
}
